package cn.da.shuai.cool.finance.controller;

import cn.da.shuai.cool.common.entity.R;
import cn.da.shuai.cool.finance.model.CoolBudget;
import cn.da.shuai.cool.finance.service.ICoolBudgetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/finance/budget")
@Tag(name = "预算管理")
public class CoolBudgetController {

    @Autowired
    private ICoolBudgetService budgetService;

    @PostMapping("/create")
    @Operation(summary = "创建预算")
    public R<Boolean> createBudget(@RequestBody CoolBudget budget) {
        return R.ok(budgetService.createBudget(budget));
    }

    @PutMapping("/update")
    @Operation(summary = "更新预算")
    public R<Boolean> updateBudget(@RequestBody CoolBudget budget) {
        return R.ok(budgetService.updateBudget(budget));
    }

    @PostMapping("/approve/{budgetId}")
    @Operation(summary = "审批预算")
    public R<Boolean> approveBudget(@PathVariable Long budgetId, @RequestParam Long approverId) {
        return R.ok(budgetService.approveBudget(budgetId, approverId));
    }

    @PostMapping("/reject/{budgetId}")
    @Operation(summary = "拒绝预算")
    public R<Boolean> rejectBudget(@PathVariable Long budgetId, @RequestParam Long approverId, @RequestParam String reason) {
        return R.ok(budgetService.rejectBudget(budgetId, approverId, reason));
    }

    @PostMapping("/record-actual/{budgetId}")
    @Operation(summary = "记录实际金额")
    public R<Boolean> recordActual(@PathVariable Long budgetId, @RequestParam BigDecimal actualAmount) {
        return R.ok(budgetService.recordActual(budgetId, actualAmount));
    }

    @GetMapping("/department/{departmentId}")
    @Operation(summary = "获取部门预算")
    public R<List<CoolBudget>> getDepartmentBudgets(@PathVariable Long departmentId, @RequestParam Integer fiscalYear) {
        return R.ok(budgetService.getDepartmentBudgets(departmentId, fiscalYear));
    }

    @GetMapping("/variance-report")
    @Operation(summary = "获取预算差异报告")
    public R<Map<String, Object>> getBudgetVarianceReport(@RequestParam Integer fiscalYear) {
        return R.ok(budgetService.getBudgetVarianceReport(fiscalYear));
    }

    @GetMapping("/trends")
    @Operation(summary = "获取预算趋势")
    public R<Map<Integer, Map<String, BigDecimal>>> getBudgetTrends(@RequestParam Integer fiscalYear) {
        return R.ok(budgetService.getBudgetTrends(fiscalYear));
    }

    @GetMapping("/department-comparison")
    @Operation(summary = "获取部门预算比较")
    public R<Map<String, Object>> getDepartmentComparison(@RequestParam Integer fiscalYear) {
        return R.ok(budgetService.getDepartmentComparison(fiscalYear));
    }

    @GetMapping("/forecast-vs-actual")
    @Operation(summary = "获取预测与实际比较")
    public R<List<Map<String, Object>>> getForecastVsActual(@RequestParam Integer fiscalYear) {
        return R.ok(budgetService.getForecastVsActual(fiscalYear));
    }
}